package frame.laxcen.test;

import frame.laxcen.loop.demo.LooperReader_Demo_0;
import frame.laxcen.loop.demo.LooperReader_Demo_2;
import frame.laxcen.loop.demo.LooperReader_Demo_3;
import frame.laxcen.loop.demo.LooperReader_Demo_4;
import frame.util.ByteArrayUtil;

public class LooperReader_Sample_Read {

    private static void test1() { // 乱序的 pid

        System.out.println("乱序的 pid");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04,
                (byte) 0x02, (byte) 0x05, (byte) 0x06,
                (byte) 0x01, (byte) 0x07, (byte) 0x08
        };

        LooperReader_Demo_0 reader = new LooperReader_Demo_0();

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    private static void test2() { // 缺失的 pid

        System.out.println("缺失的 pid");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04,
                (byte) 0x02, (byte) 0x05, (byte) 0x06
        };

        LooperReader_Demo_0 reader = new LooperReader_Demo_0();

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    private static void test3() { // 完全没有 pid

        System.out.println("完全没有 pid");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04
        };

        LooperReader_Demo_0 reader = new LooperReader_Demo_0();

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    private static void test4() { // break 语意 读取任意一个就退出

        System.out.println("break 语意 读取任意一个就退出");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04,
                (byte) 0x02, (byte) 0x05, (byte) 0x06,
                (byte) 0x01, (byte) 0x07, (byte) 0x08
        };

        LooperReader_Demo_2 reader = new LooperReader_Demo_2();

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    private static void test5() { // if 语意

        System.out.println("if 语意 Demo3");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04,
                (byte) 0x02, (byte) 0x05, (byte) 0x06,
                (byte) 0x01, (byte) 0x07, (byte) 0x08,
                (byte) 0x03, (byte) 0x09, (byte) 0x0a,
                (byte) 0x04, (byte) 0x0b, (byte) 0x0c
        };

        LooperReader_Demo_3 reader = new LooperReader_Demo_3();

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    private static void test6() { // if 语意

        System.out.println("if 语意 Demo4");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04,
                (byte) 0x02, (byte) 0x05, (byte) 0x06,
                (byte) 0x01, (byte) 0x07, (byte) 0x08,
                (byte) 0x03, (byte) 0x09, (byte) 0x0a,
                (byte) 0x04, (byte) 0x0b, (byte) 0x0c
        };

        LooperReader_Demo_4 reader = new LooperReader_Demo_4();

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    private static void test7() { // 重复使用 reader.decode

        System.out.println("重复使用 reader.decode");

        byte[] data = {
                (byte) 0xaa,
                (byte) 0x01, (byte) 0x02,
                (byte) 0x03, (byte) 0x04,
                (byte) 0x02, (byte) 0x05, (byte) 0x06,
                (byte) 0x01, (byte) 0x07, (byte) 0x08,
                (byte) 0x03, (byte) 0x09, (byte) 0x0a,
                (byte) 0x04, (byte) 0x0b, (byte) 0x0c
        };

        LooperReader_Demo_4 reader = new LooperReader_Demo_4();

        System.out.println("-- 1");

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));

        System.out.println("-- 2");

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));

        System.out.println("-- 3");

        reader.decode(data);

        reader.getResult().forEach((k, v) -> System.out.println(k + "->" + v));
    }

    public static void main(String[] args) {

        test1();

        test2();

        test3();

        test4();

        test5();

        test6();

        test7();
    }
}

